﻿using System.Security.Cryptography;
using System.Text;

namespace Admin.Crypto.Utils
{
    /// <summary>
    /// ASE安全工具类
    /// </summary>
    public class AESUtils
    {
        private static readonly string encryptionIV = "#admin*crypto#Password@@$";
        private static readonly string encryptionPassword = "~!admin#crypto^kj*password";

        /// <summary>
        /// 使用默认秘钥，默认向量加密
        /// </summary>
        /// <param name="text">明文</param>
        /// <returns></returns>
        public static string AESEncrypt(string text)
        {
            return AESEncrypt(text, encryptionPassword, encryptionIV);
        }

        /// <summary>
        /// 使用默认秘钥，默认向量解密
        /// </summary>
        /// <param name="text">密文</param>
        /// <returns></returns>
        public static string AESDecrypt(string text)
        {
            return AESDecrypt(text, encryptionPassword, encryptionIV);
        }

        /// <summary>
        /// 使用默认向量加密
        /// </summary>
        /// <param name="text">明文</param>
        /// <param name="password">秘钥</param>
        /// <returns></returns>
        public static string AESEncrypt(string text, string password)
        {
            return AESEncrypt(text, password, encryptionIV);
        }

        /// <summary>
        /// 使用默认向量解密
        /// </summary>
        /// <param name="text">密文</param>
        /// <param name="password">秘钥</param>
        /// <returns></returns>
        public static string AESDecrypt(string text, string password)
        {
            return AESDecrypt(text, password, encryptionIV);
        }

        /// <summary>
        /// AES加密 
        /// </summary>
        /// <param name="text">明文</param>
        /// <param name="password">密钥</param>    
        /// <param name="strIV">向量</param>
        /// <returns></returns>
        public static string AESEncrypt(string text, string password, string strIV)
        {
            using (var rijndaelManaged = Aes.Create())
            {
                rijndaelManaged.Mode = CipherMode.CBC;
                rijndaelManaged.Padding = PaddingMode.PKCS7;
                rijndaelManaged.KeySize = 128;
                rijndaelManaged.BlockSize = 128;

                var pwdBytes = Encoding.UTF8.GetBytes(password);
                var keyBytes = new byte[16];
                var len = pwdBytes.Length;

                if (len > keyBytes.Length)
                {
                    len = keyBytes.Length;
                }

                Array.Copy(pwdBytes, keyBytes, len);
                rijndaelManaged.Key = keyBytes;

                var ivBytes = Encoding.UTF8.GetBytes(strIV);
                var definedIVBytes = new byte[16];
                Array.Copy(ivBytes, definedIVBytes, 16);

                rijndaelManaged.IV = definedIVBytes;

                var iCryptoTransform = rijndaelManaged.CreateEncryptor();
                var textBytes = Encoding.UTF8.GetBytes(text);
                var targetBytes = iCryptoTransform.TransformFinalBlock(textBytes, 0, textBytes.Length);

                return Convert.ToBase64String(targetBytes);
            }
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="text">密文</param>
        /// <param name="password">密钥</param>     
        /// <param name="strIV">向量</param>
        /// <returns></returns>
        public static string AESDecrypt(string text, string password, string strIV)
        {
            using (var rijndaelManaged = Aes.Create())
            {
                rijndaelManaged.Mode = CipherMode.CBC;
                rijndaelManaged.Padding = PaddingMode.PKCS7;
                rijndaelManaged.KeySize = 128;
                rijndaelManaged.BlockSize = 128;

                var encryptedData = Convert.FromBase64String(text);
                var pwdBytes = Encoding.UTF8.GetBytes(password);
                var keyBytes = new byte[16];
                int len = pwdBytes.Length;

                if (len > keyBytes.Length)
                {
                    len = keyBytes.Length;
                }

                Array.Copy(pwdBytes, keyBytes, len);
                rijndaelManaged.Key = keyBytes;

                var ivBytes = Encoding.UTF8.GetBytes(strIV);
                var definedIVBytes = new byte[16];
                Array.Copy(ivBytes, definedIVBytes, 16);
                rijndaelManaged.IV = definedIVBytes;

                var iCryptoTransform = rijndaelManaged.CreateDecryptor();
                var targetBytes = iCryptoTransform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

                return Encoding.UTF8.GetString(targetBytes);
            }
        }
    }
}
